//
//  ViewController.m
//  QQMV
//
//  Created by yh on 16/8/12.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#import "BaseModel.h"
#import "MV_qq.h"
#import "Contents.h"
#import "Mv_show.h"
#import "YMJCollectionViewCell.h"
#import "YMJflowlayout.h"
#import "SupperCollectionViewCell.h"
#import "CircleCollectionView.h"


@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,CircleCollectionViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UICollectionView *collect;
@property (nonatomic, strong) MV_qq *qq;
@property (nonatomic, strong) NSMutableArray *imageArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createData];
    [self createView];
    
}
- (void)createView {
    YMJflowlayout *flowLayout = [[YMJflowlayout alloc] init];
    self.collect = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    self.collect.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collect];
    _collect.delegate = self;
    _collect.dataSource = self;
    _collect.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    [_collect registerNib:[UINib nibWithNibName:@"YMJCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"1"];
    [_collect registerNib:[UINib nibWithNibName:@"SupperCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"2"];
    // 榜单 现场 频道
    [self createBXPView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _qq.data_objc.contents_objc.list_array[section].mvlist_array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YMJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.row == 0) {
        SupperCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"2" forIndexPath:indexPath];
        cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].picurl]]];
        cell.title_ymj.text = [_qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].mvtitle stringByAppendingString:[NSString stringWithFormat:@" - %@",_qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].singername]];
        cell.desc_ymj.text = _qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].mvdesc;
        cell.labelName_ymj.text = _qq.data_objc.contents_objc.list_array[indexPath.section].name;
        return cell;
    } else {
        cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].picurl]]];
        cell.title_ymj.text = [_qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].mvtitle stringByAppendingString:[NSString stringWithFormat:@" - %@",_qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].singername]];
        cell.desc_ymj.text = _qq.data_objc.contents_objc.list_array[indexPath.section].mvlist_array[indexPath.row].mvdesc;
        return cell;
    }
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    // 处理图片
    return _qq.data_objc.contents_objc.list_array.count;
}
- (void)createData {
    NSString *urlStr = [NSString stringWithFormat:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1"];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       dispatch_async(dispatch_get_main_queue(), ^{
           NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
           NSString *newStr = [str substringFromIndex:7];
           newStr = [newStr substringToIndex:newStr.length - 1];
           NSData *aData = [newStr dataUsingEncoding:NSUTF8StringEncoding];
           NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableLeaves error:nil];
           self.qq = [MV_qq modelWithDic:dic];
            //轮播图
           _imageArray = [NSMutableArray arrayWithArray:_qq.data_objc.mv_show_playArray];
           CircleCollectionView *circleView = [[CircleCollectionView alloc] initWithFrame:CGRectMake(10, -280, 300, 200) imageArray:_imageArray];
           [self.collect addSubview:circleView];
          // 签订协议
           circleView.dataSource = self;
           [self.collect reloadData];
       });
    }];
    [task resume];
}
// 实现协议方法
- (void)handleImageView:(CustomCollectionViewCell *)cell objc:(id)objc {
    Mv_show *ms = objc;
    cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ms.picurl]]];
}
- (void)createBXPView {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, - 50, 32, 32)];
    [_collect addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"01.jpg"];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(67, - 50, 50, 32)];
    [_collect addSubview:label];
    label.text = @"榜单";
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(99 + 20, - 50, 32, 32)];
    [_collect addSubview:imageView1];
    imageView1.image = [UIImage imageNamed:@"02.jpg"];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(89 + 30 + 32 + 5, - 50, 50, 32)];
    [_collect addSubview:label1];
    label1.text = @"现场";
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(89 + 30 + 32 + 32 + 5 + 30, - 50, 32, 32)];
    [_collect addSubview:imageView2];
    imageView2.image = [UIImage imageNamed:@"03.jpg"];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(89 + 30 + 32 + 32 + 5 + 30 + 32 + 5, - 50 ,  50, 32)];
    [_collect addSubview:label2];
    label2.text = @"频道";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
